package vivek.project.pocketmoney.common.logger
{
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.filesystem.File;
	
	import mx.logging.AbstractTarget;
	import mx.logging.LogEvent;
	
	import vivek.project.pocketmoney.common.CommonData;

	public class PMLogTarget extends AbstractTarget
	{
	//	var file:File = new File("logs\pm.log");
		public function PMLogTarget()
		{
			super();
		
			filters=["vivek.project.pocketmoney.*"];
		}

		override public function logEvent(event:LogEvent):void
		{
			//trace(event);
			persistLog(event.message);
		}

		private function fileLogger():void{
		
		}
		private function persistLog(message:String):void
		{
			var array:Array=message.split('\t');
			var conn:SQLConnection=CommonData.getConnection();
			var statement:SQLStatement=new SQLStatement;
			statement.sqlConnection=conn;
			statement.text="insert into auditlog (log_txnid,log_time,log_message,log_user,log_module,log_details) values (:log_txnid,:log_time,:log_message,:log_user,:log_module,:log_details)";
			if (array[2] == "undefined")
				statement.parameters[":log_txnid"]=0;
			else
				statement.parameters[":log_txnid"]=array[2];
			statement.parameters[":log_time"]=new Date();
			statement.parameters[":log_module"]=array[0];
			statement.parameters[":log_message"]=array[1];
			statement.parameters[":log_details"]="Amount:" + array[4] + "From Account:" + array[3] + "\tTransfer Account:" + array[5] + "\tCategory:" + array[6] + "\tDate:" + CommonData.getDateFormatter().format(array[7]);
			statement.parameters[":log_user"]=0;
			//statement.parameters[":log_account"]=array[3];
			//statement.parameters[":log_amount"]=array[4];
			//statement.parameters[":log_expdate"]=array[5];
			statement.execute();
			conn.close();
			LogCache.refreshLogs();
		}

		private function persistLogs(logs:Array):void
		{
			var conn:SQLConnection=CommonData.getConnection();
			var statement:SQLStatement=new SQLStatement;
			statement.sqlConnection=conn;
			statement.text="insert into auditlog (log_txnid,log_time,log_message,log_user,log_module,log_details) values (:log_txnid,:log_time,:log_message,:log_user,:log_module,:log_details)";
			conn.begin();
			for each (var message:Object in logs)
			{
				var array:Array=message.split('\t');
				if (array[2] == "undefined")
					statement.parameters[":log_txnid"]=0;
				else
					statement.parameters[":log_txnid"]=array[2];
				statement.parameters[":log_time"]=new Date();
				statement.parameters[":log_module"]=array[0];
				statement.parameters[":log_message"]=array[1];
				statement.parameters[":log_details"]="Amount:" + array[4] + "From Account:" + array[3] + "\tTransfer Account:" + array[5] + "\tCategory:" + array[6] + "\tDate:" + CommonData.getDateFormatter().format(array[7]);
				statement.parameters[":log_user"]=0;
				statement.execute();
					//statement.parameters[":log_account"]=array[3];
					//statement.parameters[":log_amount"]=array[4];
					//statement.parameters[":log_expdate"]=array[5];
			}
			conn.commit();
			conn.close();
			LogCache.refreshLogs();
		}
	}
}